package com.example.skycodes.servlet;

import com.example.skycodes.bean.AdminBean;
import com.example.skycodes.dao.AdminDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 1.接受post网址/adminAdd.jsp
 * 2.接受到到传递过来的新闻参数 新闻标题 新闻内容
 * 3.完成新闻的添加
 * 4.跳转到其他网页
 */
@WebServlet("/adminAdd.jsp")
public class AdminAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String user =(String) req.getSession().getAttribute("User");//获取会话
        if (user==null || user.equals("")){
            resp.sendRedirect("login.jsp");//没登录 跳转到登陆页面
            return;
        }
        String adminPwd = req.getParameter("AdminPwd");
        String adminName= req.getParameter("AdminName");
        AdminBean adminBean = new AdminBean(); //实例化新闻对象
        adminBean.setAdminPwd(adminPwd);;//设置新闻标题
        adminBean.setAdminName(adminName);
        ;//设置新闻内容
        adminBean.setAdminName("Admin");
        AdminDao adminDao = new AdminDao();//实例化 新闻DAO
        adminDao.add(adminBean);//调用添加方法 传递新闻对象
        //使用网址跳转 请求结果对象
        resp.sendRedirect("admin.jsp");
    }//

}
